package com.dzsw.redis.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class JsonUtil {
    private static Logger logger = LoggerFactory.getLogger(JsonUtil.class);
    private static ObjectMapper mapper = new ObjectMapper();
    static{
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }
    /**
     * 判断字符串是否JSON格式
     *
     * @param str 要判断的字符串
     */
    public static boolean isJson(String str) {
        if (!str.startsWith("{") || !str.startsWith("[")) {
            return false;
        }
        try {
            mapper.readTree(str);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * 把对象序列化为JSON
     *
     * @param object 传入对象
     * @return JSON字符串
     */
    public static String toJson(Object object) {
        try {
            return mapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            logger.error("[toJsonError]:{}",e);
            return "";
        }
    }

    /**
     * 把json对象序列化为map
     *
     * @param json json字符串
     * @return hashmap对象
     * @throws IOException
     */
    public static Map<String, Object> jsonToMap(String json)   {
        try {
            return mapper.readValue(json, HashMap.class);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    /**
     * 把json对象序列化为map
     *
     * @param json json字符串
     * @return hashmap对象
     * @throws IOException
     */
    public static <T>  T jsonToObject(String json,Class<T> classz)   {
        try {
            return mapper.readValue(json, classz);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    public static void main(String [] args ){
//        try {
//            String str = "{\"success\":true,\"data\":[{\"orderInfo\":{\"orderId\":\"437662701221711874\",\"pushOutStatus\":0,\"payTime\":\"\",\"orderTotalPrice\":3000,\"orderCreateTime\":\"2019-09-11 13:50\",\"orderPayType\":1,\"deviceCode\":\"SB001\",\"productCount\":1,\"pushOutCode\":\"\",\"deviceName\":\"自助机1号\",\"deviceAddress\":\"中润国际大厦\",\"cityName\":\"沈阳市\",\"deviceFullAdress\":\"辽宁省沈阳市沈河区中润国际大厦\",\"productImgList\":\"http://pfks-oss-center.oss-cn-beijing.aliyuncs.com/BD1A1D3033EB4EB790927D0EC5B8C88E.image/jpeg\",\"provinceName\":\"辽宁省\",\"payStatus\":0,\"goodsNameList\":\"肠胃感冒1型\",\"countyName\":\"沈河区\"},\"orderProductInfo\":[{\"specificationId\":\"437602968297148453\",\"orderId\":\"437662701221711874\",\"productTotalPrice\":3000,\"topImg\":\"http://pfks-oss-center.oss-cn-beijing.aliyuncs.com/BD1A1D3033EB4EB790927D0EC5B8C88E.image/jpeg\",\"productCount\":1,\"productName\":\"肠胃感冒1型\",\"specificationName\":\"肠胃感冒1型-02\",\"productPrice\":3000}]}],\"total\":\"0\",\"message\":\"\",\"code\":\"\",\"traceId\":\"365b5da617264df0949b49c486cc835e\",\"other\":\"\",\"hashMore\":false}";
//            OrderDetailResponse orderDetailResponse = (OrderDetailResponse) JsonUtil.jsonToObject(str, OrderDetailResponse.class);
//            System.out.println();
//        }catch (Exception e){
//            e.printStackTrace();
//        }
    }

}
